﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 干部工作 意见建议
    /// </summary>
    public class PartyCadresWorkSuggest : EntityBase
    {
        #region 字段属性

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 组织ID（保留）
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 联系方式
        /// </summary>
        public string ContactWay { get; set; }

        /// <summary>
        /// 是否显示
        /// </summary>
        public bool? IsVisible { get; set; }

        /// <summary>
        /// 0 待回复  1 已回复
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 回得内容 
        /// </summary>
        public string Reply { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }

        #endregion

        #region 查询管理
        public static DataSet GetPortalListData(int pageIndex, int pageSize, string title, string status)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {

                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex) * pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };

            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  *,if(Status=1,'已回复','待回复') as StatusName from tblpartycadresworksuggest where 1=1");
            if (!string.IsNullOrEmpty(title))
            {
                sb.Append(" and Title like ?Title ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "Title", Value = string.Format("%{0}%", title) });
            }
            if (!string.IsNullOrEmpty(status))
            {
                sb.Append(" and Status=?Status");
                parameters.Add(new MySqlParameter() { DbType = DbType.Int32, ParameterName = "Status", Value = status });
            }
           
            sb.Append(" order by Status, CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetGridData(int pageIndex, int pageSize, string title, string branchId)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  *,if(Status=1,'已回复','待回复') as StatusName from tblpartycadresworksuggest where 1=1");

            if (!string.IsNullOrEmpty(title))
            {
                sb.Append(" and Title like ?Title ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Title",
                        Value = string.Format("%{0}%", title)
                    });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sb.Append(" and BranchID in (select ChildBranchID from tblpartyorgbranchrelation brr where brr.BranchID=?BranchID) ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "BranchID",
                        Value = string.Format("{0}", branchId)
                    });
            }

            sb.Append(" order by Status, CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
